<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Input;
use App\Models\BbsAnswer;
use App\Models\BbsCategory;
use App\Models\BbsQuestion;
use Illuminate\Support\Facades\DB;
use App\Models\BbsAnswerComment;

class BbsAnswerController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function answerList()
    {
        if(!$this->user->can('viewBbsAnswer')) abort(405);
        $query_arr = array_except(Input::all(), ['_token']);
        $bbs_ques = new BbsAnswer();
        if (isset($query_arr['keywords']) AND $query_arr['keywords'] != '')
            $bbs_ques = $bbs_ques->where('title', 'like', "%" . $query_arr['keywords'] . "%");
        $answers = $bbs_ques->paginate(20);
        foreach ($answers as $key => $val) {
            $answers[$key]['question_name'] = $val->getQuestionByAnswer['name'];
        }
        /*分类列表*/
        $question = BbsQuestion::all();
        $bbs_cat = BbsCategory::all();
        return view('admin.bbs.answers.index')
                ->with('answers', $answers)
                ->with('bbs_cat', $bbs_cat)
                ->with('question',$question);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        if(!$this->user->can('viewBbsAnswer')) abort(405);
        
        $question = BbsQuestion::all();
        $bbs_cat = BbsCategory::all();
        $answers = BbsAnswer::find($id);
        return view('admin.bbs.answers.edit') 
                ->with('blademod','edit')
                ->with('bbs_cat', $bbs_cat)
                ->with('question', $question)
                ->with('answers',$answers);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy(Request $request)
    {
        if(!$this->user->can('deleteBbsAnswer')) abort(405);
        
        $id = $request->input('answer_id');
        $bbs_answ = BbsAnswer::find($id);
//        if ($bbs_answ->delete())
//            return  Response::json(array('status'=>'success','msg'=>'回答 [' . str_limit($bbs_answ->title, 15,  '...') . '] 删除成功'));
//        else
//            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
        if(empty($bbs_answ))
        {
            return  $this->formatJson('falied','抱歉，资源不存在!');
        }
        else
        {
            DB::beginTransaction();
            try{
                //删除子评论,评论数减一
                BbsAnswerComment::where('answer_id', '=', $bbs_answ->id)->delete();
                $bbsq = BbsQuestion::findOrNew($bbs_answ->question_id);
                $bbsq->decrementDel('answer_count',1);
                $bbsq->decrementDel('comment_count',1);
                $bbs_answ->delete();
                DB::commit();
            }catch (\PDOException $e){
                DB::rollback();
                return $this->formatJson('falied','回答删除失败');
            }
            return $this->formatJson('success','回答删除成功！');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function deletes(Request $request)
    {
        if(!$this->user->can('deleteBbsAnswer'))
            return $this->formatJson('error','抱歉，您没有相关操作权限');
        $ids = explode(',', $request->input('id'));
        $bbs_comment = BbsAnswer::whereIn('id', $ids);
        $count = $bbs_comment->count();
        if ($bbs_comment->delete())
            return  Response::json(array('status'=>'success','msg'=>'成功删除 [' . $count . '] 评论'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }
}
